package me.hetong.caigou.ui;

import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.log.StaticLog;
import javafx.application.Platform;
import javafx.geometry.Insets;
import javafx.scene.control.Label;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import me.hetong.caigou.ui.view.Right;

import static me.hetong.caigou.Global.config;

/**
 * 提示消息
 */
public class Message {
    private static final String ERR = "运行异常,请查看日志文件,/logs";
    private static VBox vBox = new VBox();
    private static Label label = new Label();

    static {

        label.setTextFill(Color.WHITE);
        vBox.setPadding(new Insets(20, 50, 20, 50));
        vBox.getChildren().add(label);
        vBox.setVisible(false);
        vBox.setManaged(false);

        AnchorPane.setBottomAnchor(vBox, 10.0);
        AnchorPane.setRightAnchor(vBox, 10.0);

    }

    /**
     * 提示消息
     *
     * @param text
     */
    public static void info(AnchorPane anchorPane, String... text) {
        ThreadUtil.execute(() -> Platform.runLater(() -> {
            vBox.setStyle("-fx-background-color: #F3B044ee;-fx-border-radius: 5;-fx-background-radius: 5;");
            createMessage(anchorPane, text);
        }));
    }

    private static void createMessage(AnchorPane anchorPane, String[] text) {

        anchorPane.getChildren().remove(vBox);
        anchorPane.getChildren().add(vBox);

        StringBuilder sb = new StringBuilder();
        for (String s : text) {
            sb.append(s);
            sb.append(" ");
        }

        StaticLog.info("{}", sb);

        label.setText(sb.toString());
        vBox.setVisible(true);
        vBox.setManaged(true);

        ThreadUtil.execute(() -> {
            ThreadUtil.sleep(config.getMessageWaitingTime());
            vBox.setVisible(false);
            vBox.setManaged(false);
        });
    }
}